﻿package core.ui.dialogs
{
    import __AS3__.vec.*;
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class LevelsDialog extends BitmapOperationDialogBase
    {
        var channel:Dropdown;
        var levelsin:Sprite;
        var levelsout:Sprite;
        var bmr:Bitmap;
        var bmg:Bitmap;
        var bmb:Bitmap;
        var bma:Bitmap;
        var bmo:Bitmap;
        var minin:ColorDrag;
        var midin:ColorDrag;
        var maxin:ColorDrag;
        var minout:ColorDrag;
        var maxout:ColorDrag;
        var ccm:ChannelMap;
        var cmRGB:ChannelMap;
        var cmR:ChannelMap;
        var cmG:ChannelMap;
        var cmB:ChannelMap;
        var ltminin:Literal;
        var ltmidin:Literal;
        var ltmaxin:Literal;
        var ltminout:Literal;
        var ltmaxout:Literal;
        var cd:ColorDrag;

        public function LevelsDialog(param1:Tuyitu)
        {
            super(param1, "rm_levels", 290, 330, Action.LevelsAdjustment);
            var _loc_2:* = new Literal("rm_input_levels");
            _loc_2.x = 13;
            _loc_2.y = 62;
            addChild(_loc_2);
            var _loc_3:* = new Sprite();
            _loc_3.x = 15;
            _loc_3.y = 80;
            _loc_3.graphics.beginFill(8421504);
            _loc_3.graphics.drawRect(0, 0, 260, 104);
            _loc_3.graphics.beginFill(15658734);
            _loc_3.graphics.drawRect(1, 1, 258, 102);
            _loc_3.graphics.endFill();
            addChild(_loc_3);
            var _loc_4:* = new Literal("rm_channel");
            _loc_4.x = 13;
            _loc_4.y = 30;
            addChild(_loc_4);
            this.channel = new Dropdown(150, 150);
            this.channel.addEventListener(SelectEvent.COMMAND, this.ChannelChange);
            this.channel.AddItem(U.RM("rm_rgb"), "rgb", true);
            this.channel.AddItem(U.RM("rm_red"), "red");
            this.channel.AddItem(U.RM("rm_green"), "green");
            this.channel.AddItem(U.RM("rm_blue"), "blue");
            this.channel.Bind();
            this.channel.x = _loc_4.x + _loc_4.width + 5;
            this.channel.y = 30;
            addChild(this.channel);
            this.bmr = new Bitmap(new BitmapData(256, 100, true, 16777215));
            this.bmr.x = 2;
            this.bmr.y = 2;
            this.bmr.alpha = 0.5;
            _loc_3.addChild(this.bmr);
            this.bmg = new Bitmap(new BitmapData(256, 100, true, 16777215));
            this.bmg.x = 2;
            this.bmg.y = 2;
            this.bmg.alpha = 0.5;
            _loc_3.addChild(this.bmg);
            this.bmb = new Bitmap(new BitmapData(256, 100, true, 16777215));
            this.bmb.x = 2;
            this.bmb.y = 2;
            this.bmb.alpha = 0.5;
            _loc_3.addChild(this.bmb);
            this.bma = new Bitmap(new BitmapData(256, 100, true, 16777215));
            this.bma.x = 2;
            this.bma.y = 2;
            this.bma.alpha = 0.9;
            _loc_3.addChild(this.bma);
            var _loc_5:* = new Literal("rm_output_levels");
            _loc_5.x = 13;
            _loc_5.y = 222;
            addChild(_loc_5);
            var _loc_6:* = new Sprite();
            _loc_6.x = 15;
            _loc_6.y = 240;
            _loc_6.graphics.beginFill(9474192);
            _loc_6.graphics.drawRect(0, 0, 260, 14);
            _loc_6.graphics.beginFill(15658734);
            _loc_6.graphics.drawRect(1, 1, 258, 12);
            _loc_6.graphics.endFill();
            addChild(_loc_6);
            this.bmo = new Bitmap(new BitmapData(256, 10));
            this.bmo.x = 2;
            this.bmo.y = 2;
            _loc_6.addChild(this.bmo);
            this.levelsin = new Sprite();
            this.levelsin.x = 13;
            this.levelsin.y = 185;
            addChild(this.levelsin);
            this.minin = new ColorDrag(0);
            this.minin.addEventListener(MouseEvent.MOUSE_DOWN, this.LevelMouseDown);
            this.levelsin.addChild(this.minin);
            this.midin = new ColorDrag(8947848);
            this.midin.addEventListener(MouseEvent.MOUSE_DOWN, this.LevelMouseDown);
            this.midin.x = 128;
            this.levelsin.addChild(this.midin);
            this.maxin = new ColorDrag(16777215);
            this.maxin.addEventListener(MouseEvent.MOUSE_DOWN, this.LevelMouseDown);
            this.maxin.x = 255;
            this.levelsin.addChild(this.maxin);
            this.ltminin = new Literal("0");
            this.ltminin.x = 15;
            this.ltminin.y = 200;
            addChild(this.ltminin);
            this.ltmidin = new Literal("1.00");
            this.ltmidin.x = 135;
            this.ltmidin.y = 200;
            addChild(this.ltmidin);
            this.ltmaxin = new Literal("255");
            this.ltmaxin.x = 250;
            this.ltmaxin.y = 200;
            addChild(this.ltmaxin);
            this.levelsout = new Sprite();
            this.levelsout.x = 13;
            this.levelsout.y = 255;
            addChild(this.levelsout);
            this.minout = new ColorDrag(0);
            this.minout.addEventListener(MouseEvent.MOUSE_DOWN, this.LevelMouseDown);
            this.levelsout.addChild(this.minout);
            this.maxout = new ColorDrag(16777215);
            this.maxout.addEventListener(MouseEvent.MOUSE_DOWN, this.LevelMouseDown);
            this.maxout.x = 255;
            this.levelsout.addChild(this.maxout);
            this.ltminout = new Literal("0");
            this.ltminout.x = 15;
            this.ltminout.y = 270;
            addChild(this.ltminout);
            this.ltmaxout = new Literal("255");
            this.ltmaxout.x = 250;
            this.ltmaxout.y = 270;
            addChild(this.ltmaxout);
            this.DrawOutput();
            if (!locked)
            {
                this.AnalyzeImage();
            }
            this.cmRGB = new ChannelMap();
            this.cmR = new ChannelMap();
            this.cmG = new ChannelMap();
            this.cmB = new ChannelMap();
            this.ccm = this.cmRGB;
            return;
        }// end function

        private function AnalyzeImage() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:uint = 0;
            var _loc_5:int = 0;
            var _loc_10:int = 0;
            var _loc_6:* = new Vector.<int>(256, true);
            var _loc_7:* = new Vector.<int>(256, true);
            var _loc_8:* = new Vector.<int>(256, true);
            var _loc_9:* = new Vector.<int>(256, true);
            while (_loc_10 < bm.width)
            {
                
                _loc_5 = 0;
                while (_loc_5 < bm.height)
                {
                    
                    _loc_4 = bm.getPixel32(_loc_10, _loc_5);
                    if ((_loc_4 >>> 24 & 255) > 0)
                    {
                        _loc_1 = _loc_4 >>> 16 & 255;
                        _loc_2 = _loc_4 >>> 8 & 255;
                        _loc_3 = _loc_4 & 255;
                        (_loc_9[_loc_1] + 1);
                        (_loc_9[_loc_2] + 1);
                        (_loc_9[_loc_3] + 1);
                        (_loc_6[_loc_1] + 1);
                        (_loc_7[_loc_2] + 1);
                        (_loc_8[_loc_3] + 1);
                    }
                    _loc_5++;
                }
                _loc_10++;
            }
            this.DrawLevels(_loc_9, this.bma.bitmapData, 4278190080);
            this.DrawLevels(_loc_6, this.bmr.bitmapData, 4294901760);
            this.DrawLevels(_loc_7, this.bmg.bitmapData, 4278255360);
            this.DrawLevels(_loc_8, this.bmb.bitmapData, 4278190335);
            return;
        }// end function

        private function DrawLevels(MouseEvent:Vector.<int>, MouseEvent:BitmapData, MouseEvent:uint) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_10:int = 0;
            var _loc_12:int = 0;
            var _loc_8:* = bm.width * bm.height;
            var _loc_9:* = new Array(0, 0, 0, 0);
            while (_loc_10 < MouseEvent.length)
            {
                
                _loc_6 = 0;
                while (_loc_6 < 4)
                {
                    
                    if (_loc_9[_loc_6] < MouseEvent[_loc_10])
                    {
                        _loc_9[_loc_6] = MouseEvent[_loc_10];
                        break;
                    }
                    _loc_6++;
                }
                _loc_10++;
            }
            _loc_5 = _loc_9[0];
            var _loc_11:* = 1;
            while (_loc_11 < 4)
            {
                
                if (_loc_5 > _loc_9[_loc_11])
                {
                    _loc_5 = _loc_9[_loc_11];
                }
                _loc_11 = _loc_11 + 1;
            }
            while (_loc_12 < 256)
            {
                
                _loc_4 = Math.round(1 * MouseEvent[_loc_12] * 100 / _loc_5);
                if (_loc_4 > 100)
                {
                    _loc_4 = 100;
                }
                _loc_7 = _loc_4;
                while (_loc_7 >= 0)
                {
                    
                    MouseEvent.setPixel32(_loc_12, 100 - _loc_7, MouseEvent);
                    _loc_7 = _loc_7 - 1;
                }
                _loc_12++;
            }
            return;
        }// end function

        private function DrawOutput() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            while (_loc_2 < this.bmo.width)
            {
                
                _loc_1 = 0;
                while (_loc_1 < this.bmo.height)
                {
                    
                    this.bmo.bitmapData.setPixel(_loc_2, _loc_1, _loc_2 << 16 | _loc_2 << 8 | _loc_2);
                    _loc_1++;
                }
                _loc_2++;
            }
            return;
        }// end function

        private function ChannelChange(event:SelectEvent) : void
        {
            switch(event.val)
            {
                case "rgb":
                {
                    this.bma.visible = true;
                    this.bma.alpha = 0.9;
                    this.bmr.visible = true;
                    this.bmr.alpha = 0.3;
                    this.bmg.visible = true;
                    this.bmg.alpha = 0.3;
                    this.bmb.visible = true;
                    this.bmb.alpha = 0.3;
                    this.ccm = this.cmRGB;
                    break;
                }
                case "red":
                {
                    this.bmr.visible = true;
                    this.bmr.alpha = 0.9;
                    this.bma.visible = false;
                    this.bmg.visible = false;
                    this.bmb.visible = false;
                    this.ccm = this.cmR;
                    break;
                }
                case "green":
                {
                    this.bmg.visible = true;
                    this.bmg.alpha = 0.9;
                    this.bma.visible = false;
                    this.bmr.visible = false;
                    this.bmb.visible = false;
                    this.ccm = this.cmG;
                    break;
                }
                case "blue":
                {
                    this.bmb.visible = true;
                    this.bmb.alpha = 0.9;
                    this.bma.visible = false;
                    this.bmr.visible = false;
                    this.bmg.visible = false;
                    this.ccm = this.cmB;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.ltminin.text = this.ccm.minin.toString();
            this.ltmaxin.text = this.ccm.maxin.toString();
            this.ltmidin.text = (Math.round(this.ccm.midin * 100) / 100).toString();
            this.ltminout.text = this.ccm.minout.toString();
            this.ltmaxout.text = this.ccm.maxout.toString();
            this.minin.x = this.ccm.minin;
            this.maxin.x = this.ccm.maxin;
            this.minout.x = this.ccm.minout;
            this.maxout.x = this.ccm.maxout;
            this.midin.x = Math.round((this.ccm.maxin - this.ccm.minin) * this.ccm.mid + this.ccm.midin);
            return;
        }// end function

        private function LevelMouseDown(event:MouseEvent) : void
        {
            this.cd = event.currentTarget as ColorDrag;
            this.cd.Mark(true);
            switch(this.cd)
            {
                case this.minin:
                {
                    this.cd.startDrag(false, new Rectangle(0, 0, this.maxin.x - 10, 0));
                    break;
                }
                case this.maxin:
                {
                    this.cd.startDrag(false, new Rectangle(this.minin.x + 10, 0, 255 - (this.minin.x + 10), 0));
                    break;
                }
                case this.midin:
                {
                    this.cd.startDrag(false, new Rectangle(this.minin.x + 5, 0, this.maxin.x - 5 - (this.minin.x + 5), 0));
                    break;
                }
                case this.minout:
                {
                    this.cd.startDrag(false, new Rectangle(0, 0, this.maxout.x - 10, 0));
                    break;
                }
                case this.maxout:
                {
                    this.cd.startDrag(false, new Rectangle(this.minout.x + 10, 0, 255 - (this.minout.x + 10), 0));
                    break;
                }
                default:
                {
                    break;
                }
            }
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.LevelMouseMove, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.LevelMouseUp, false, 1, true);
            return;
        }// end function

        private function LevelMouseMove(event:MouseEvent) : void
        {
            switch(this.cd)
            {
                case this.minin:
                {
                    this.ccm.minin = Math.round(this.cd.x);
                    this.ltminin.text = this.ccm.minin.toString();
                    break;
                }
                case this.maxin:
                {
                    this.ccm.maxin = Math.round(this.cd.x);
                    this.ltmaxin.text = this.ccm.maxin.toString();
                    break;
                }
                case this.midin:
                {
                    this.ccm.SetMid(Math.round(this.cd.x));
                    this.ltmidin.text = (Math.round(this.ccm.midin * 100) / 100).toString();
                    break;
                }
                case this.minout:
                {
                    this.ccm.minout = Math.round(this.cd.x);
                    this.ltminout.text = this.ccm.minout.toString();
                    break;
                }
                case this.maxout:
                {
                    this.ccm.maxout = Math.round(this.cd.x);
                    this.ltmaxout.text = this.ccm.maxout.toString();
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this.cd == this.minin || this.cd == this.maxin)
            {
                this.midin.x = Math.round((this.ccm.maxin - this.ccm.minin) * this.ccm.mid + this.ccm.minin);
            }
            this.Change();
            return;
        }// end function

        private function LevelMouseUp(event:MouseEvent) : void
        {
            this.cd.Mark(false);
            this.cd.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.LevelMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.LevelMouseUp, false);
            return;
        }// end function

        override public function Change(... args) : void
        {
            if (!locked)
            {
                bm.copyPixels(bc, bm.rect, bm.rect.topLeft);
                if (!this.cmR.flat || !this.cmG.flat || !this.cmB.flat)
                {
                    bm.paletteMap(bm, bm.rect, bm.rect.topLeft, this.cmR.flat ? (null) : (this.cmR.ComputeArray(16)), this.cmG.flat ? (null) : (this.cmG.ComputeArray(8)), this.cmB.flat ? (null) : (this.cmB.ComputeArray(0)));
                }
                if (!this.cmRGB.flat)
                {
                    bm.paletteMap(bm, bm.rect, bm.rect.topLeft, this.cmRGB.ComputeArray(16), this.cmRGB.ComputeArray(8), this.cmRGB.ComputeArray(0));
                }
            }
            return;
        }// end function

    }
}
